﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Zo.Models
{
    /// <summary>
    /// 自定义错误异常类，用于拦截显示错误
    /// </summary>
    [Serializable]
    public class ErrorBox:Exception
    {
        /// <summary>
        /// 返回普通 显示的
        /// </summary>
        /// <param name="msg"></param>
        public ErrorBox(string msg):base(msg)
        {
            ErrorModel.Instance = new ErrorModel(msg);
        }
        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="Messager"></param>
        /// <param name="Status"></param>
        public ErrorBox(string Messager, ErrorMsgStatus Status)
           : base(Messager)
        {
            ErrorModel.Instance = new ErrorModel(Messager, Status);
        }
        
        public ErrorBox(string Messager, Exception InnerException)
            : base(Messager, InnerException)
        {
            ErrorModel.Instance = new ErrorModel(Messager);
        }

    }
    public class ErrorModel
    {
        public static ErrorModel Instance { set; get; }
        /// <summary>
        /// 错误消息
        /// </summary>
        public string msg { set; get; }

        /// <summary>
        /// 错误状态码
        /// </summary>
        public int status { set; get; }

        /// <summary>
        /// 堆栈
        /// </summary>
        public string StackTrace { get; set; }

        /// <summary>
        /// 异常引发方法
        /// </summary>
        public string TargetSite { get; set; }

        /// <summary>
        /// 异常对象或应用程序
        /// </summary>
        public string Source { get; set; }

        /// <summary>
        /// 异常信息键值对集合
        /// </summary>
        public object Data { get; set; }

        public ErrorModel(string msg)
        {
            this.msg = msg.ToString();
            this.status = (int)ErrorMsgStatus.Message;
        }

        public ErrorModel(object msg, ErrorMsgStatus msgStatus)
        {
            if (msgStatus == ErrorMsgStatus.Custom)
            {
                this.Data = msg;
                this.status = (int)msgStatus;
            }
            else
            {
                this.msg = msg.ToString();
                this.status = (int)msgStatus;
            }                    
        }

        public ErrorModel(Exception exception)
        {
            this.msg = exception.Message;
            this.status = (int)ErrorMsgStatus.Application;
            this.StackTrace = exception.StackTrace;
            this.TargetSite = exception.TargetSite != null ? exception.TargetSite.Name : null;
            this.Source = exception.Source;
            this.Data = exception.Data != null ? exception.Data.ToString() : "";
        }
    }
    /// <summary>
    /// 消息状态
    /// </summary>
    public enum ErrorMsgStatus
    {
        /// <summary>
        /// 自义定消息
        /// </summary>
        Message = 10,
        /// <summary>
        /// 登录超时
        /// </summary>
        LoginTimeOut= 20,
        /// <summary>
        /// 手动处理
        /// </summary>
        HandleSelf = 30,
        /// <summary>
        /// 程序级错误
        /// </summary>
        Application = 50,
        /// <summary>
        /// 普通错误
        /// </summary>
        Custom = 60
    }
}
